package fleditor.view.assetView
{
	import fl.containers.ScrollPane;
	
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filesystem.File;
	
	import fleditor.common.MapAssetLibLoad;
	import fleditor.controller.FlEditorController;
	import fleditor.model.FlEditorModel;
	import fleditor.utils.GetSwfAllClass;
	
	import ui.asset.assetFodderListUI;

	/**
	 *物件素材库列表
	 * @author Devin
	 * 
	 */	
	public class AssetFodderListView extends assetFodderListUI
	{
		/**
		 *主场景 
		 */		
		private var _stage:Stage;
		
		/**
		 *全局Model类(FlEditModel)
		 */		
		private var _model:FlEditorModel;
		
		/**
		 *全局控制器(FlEditorController) 
		 */		
		private var _controller:FlEditorController;
		
		/**
		 *素材库单项类名列表 
		 */		
		private var _fodderItemList:Array=[];
		
		/**
		 *列表容器 
		 */		
		private var _listBox:Sprite;
		
		private var _listScrollPane:ScrollPane;
		
		/**
		 *物件素材库列表 
		 * @param controller 全局控制器(FlEditorController) 
		 * @param model 全局Model类(FlEditModel)
		 * 
		 */		
		public function AssetFodderListView(stage:Stage, controller:FlEditorController, model:FlEditorModel)
		{
			_stage=stage;
			_controller=controller;
			_model=model;
			
			setView();
			setEvent();
		}
		
		/**
		 *设置视图 
		 * 
		 */		
		private function setView():void
		{
			if(listPos.parent) listPos.parent.removeChild(listPos);
			_listBox=new Sprite();
			_listScrollPane=new ScrollPane();
			_listScrollPane.x=listPos.x;
			_listScrollPane.y=listPos.y;
			_listScrollPane.width=155;
			_listScrollPane.height=_stage.stageHeight-90-listPos.y-3;
			_listScrollPane.source=_listBox;
			addChild(_listScrollPane);
			
			resetUI();
		}
		
		/**
		 *设置事件 
		 * 
		 */		
		private function setEvent():void
		{
			_stage.addEventListener(Event.RESIZE, __onResize);//窗口大小变化监听
			dropFodderLib.addEventListener(Event.CHANGE, __onDropFodderLibChange);
			btnAddFodderLib.addEventListener(MouseEvent.CLICK, __onAddFodderLibClick);
		}
		
		/**
		 *增加资源库配置文件 
		 */		
		private function __onAddFodderLibClick(evt:MouseEvent):void
		{
			var file:File = File.applicationDirectory;
			file.browseForOpen("请选择素材库文件");
			file.addEventListener(Event.SELECT,__onLibFileSelectHandle);
		}
		
		/**
		 *当选定完成资源库路径 
		 */		
		private function __onLibFileSelectHandle(e:Event=null):void
		{
			var arrFileName:Array=e.target.nativePath.toString().split('\\');
			var fileName:String=arrFileName[arrFileName.length-1];
			
			if(checkFodderLibPathExist(fileName)) return;
			
			_model.assetFodderPathList.push(fileName);
			dropFodderLib.addItem({label:fileName, data:e.target.nativePath});
			
			var load:MapAssetLibLoad=new MapAssetLibLoad(e.target.nativePath);
			load.addEventListener(Event.COMPLETE, __onAssetFodderLoadComplete);
		}
		
		private function __onAssetFodderLoadComplete(evt:Event):void
		{
			var load:MapAssetLibLoad=evt.currentTarget as MapAssetLibLoad;
			var arr:Array=GetSwfAllClass.getSWFClassName(load.assetLib);
			
			for(var i:int=0;i<arr.length;i++)
			{
				_fodderItemList.push(arr[i]);
			}
			
			clearItemList();
			setItemList();
		}
		
		/**
		 *检查素材库文件名是否已经存在加载了 
		 */		
		private function checkFodderLibPathExist(fileName:String):Boolean
		{
			for(var i:int=0;i<_model.assetFodderPathList.length;i++)
			{
				if(_model.assetFodderPathList[i]==fileName) return true;
			}
			return false;
		}
		
		/**
		 *当资源库发生选择时 
		 */		
		private function __onDropFodderLibChange(evt:Event):void
		{

		}
		
		/**
		 *设置单项物件列表 
		 * 
		 */		
		private function setItemList():void
		{
			for(var i:int=0;i<_fodderItemList.length;i++)
			{
				var item:AssetFodderListItemView=new AssetFodderListItemView(_fodderItemList[i]);
				item.y=_listBox.height;
				_listBox.addChild(item);
			}
			
			_listScrollPane.refreshPane();
			_listScrollPane.update();
		}
		
		/**
		 *清除单项物件列表 
		 * 
		 */		
		private function clearItemList():void
		{
			while(_listBox.numChildren)
			{
				var item:AssetFodderListItemView=_listBox.getChildAt(0) as AssetFodderListItemView;
				item.dispose();
				item=null;
			}
			
			_listScrollPane.refreshPane();
			_listScrollPane.update();
		}
		
		/**
		 * 窗口发生变化时 
		 * @param evt
		 * 
		 */		
		private function __onResize(evt:Event):void
		{
			resetUI();
		}
		
		/**
		 *设置UI 
		 * 
		 */		
		private function resetUI():void
		{
			graphics.clear();
			graphics.beginFill(0xE1E1E1);
			graphics.drawRect(0, 0, 160, _stage.stageHeight-90-3);
			graphics.lineStyle(1, 0x8c8c8c);
			graphics.moveTo(-1, 0);
			graphics.lineTo(-1, _stage.stageHeight-90-3);//stage.stageHeight-90-3=舞台高-顶部高度-底部边框高度
			graphics.endFill();
			
			_listScrollPane.height=_stage.stageHeight-90-listPos.y-3;
		}
		
		/**
		 * 资源释放 
		 * 
		 */		
		public function dispose():void
		{
			
		}
	}
}